package com.koron.common.bean.query;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.koron.util.DateUtils;
import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class BaseQueryBean {
	/**
	 * 第几页
	 */
	private int page=1;
	/**
	 * 每页条数
	 */
	private int pageCount = 20;

	/**
	 * 模糊查询字段
	 */
	private String fuzzyQuery;

	/**
	 * 模糊查询字段组
	 */
	private List<String> fuzzyItems;

	public List<String> getFuzzyItems() {
		return fuzzyItems;
	}

	public void setFuzzyItems(List<String> fuzzyItems) {
		this.fuzzyItems = fuzzyItems;
	}

	private String where;

	@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
	private Date startQueryTime;		// 查询时间中的开始时间
	@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
	private Date endQueryTime;		// 查询时间中的结束时间
	private List<String> timeQueryRange;		// 查询时间中的时间范围

	public String getWhere() {
		return where;
	}
	public void setWhere(String where) {
		this.where = where;
	}
	public String getFuzzyQuery() {
		return fuzzyQuery;
	}
	public void setFuzzyQuery(String fuzzyQuery) {
		this.fuzzyQuery = fuzzyQuery;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getPageCount() {
		return pageCount;
	}
	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	public Date getStartQueryTime() {
		return startQueryTime;
	}

	public void setStartQueryTime(Date startQueryTime) {
		this.startQueryTime = startQueryTime;
	}

	public Date getEndQueryTime() {
		return endQueryTime;
	}

	public void setEndQueryTime(Date endQueryTime) {
		this.endQueryTime = endQueryTime;
	}

	public void setTimeQueryRange(List<String> timeQueryRange) {
		if (timeQueryRange == null || timeQueryRange.size() != 2 || StringUtils.isBlank(timeQueryRange.get(0)) || StringUtils.isBlank(timeQueryRange.get(1))) {
			this.timeQueryRange = null;
		} else {
            try {
                this.setStartQueryTime(DateUtils.parseDate(timeQueryRange.get(0), "yyyy-MM-dd"));
                this.setEndQueryTime(DateUtils.parseDate(timeQueryRange.get(1), "yyyy-MM-dd"));
                this.timeQueryRange = timeQueryRange;
            } catch (ParseException e) {
                this.timeQueryRange = null;
            }
		}
	}

	public List<String> getTimeQueryRange() {
		if (startQueryTime != null && endQueryTime != null) {
			timeQueryRange = new ArrayList<>();
			timeQueryRange.add(0, DateUtils.parseDate(startQueryTime, "yyyy-MM-dd"));
			timeQueryRange.add(1, DateUtils.parseDate(endQueryTime, "yyyy-MM-dd"));
		}
		return timeQueryRange;
	}

	@Override
	public String toString() {
		return "BaseQueryBean{" +
				"page=" + page +
				", pageCount=" + pageCount +
				", fuzzyQuery='" + fuzzyQuery + '\'' +
				", fuzzyItems=" + fuzzyItems +
				", where='" + where + '\'' +
				'}';
	}
}
